/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.Decies_caseDAO;
import com.core.database.Manager;
import com.core.entities.Decies_case;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class Decies_caseDAOImpl implements Decies_caseDAO {
		Decies_case decies_case = new Decies_case();
		ArrayList<Decies_case> decies_casesList = new ArrayList<Decies_case>();

	Connection connection = null;
	private static Decies_caseDAOImpl instance=null;
	public static Decies_caseDAOImpl getInstance(){
		if(instance==null){
			instance=new Decies_caseDAOImpl();
		}
		return instance;
	}

	@Override
	public Decies_case findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM decies_case WHERE name = '" + name
					+ "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					decies_case.setId(rs.getInt(1));
					decies_case.setName(rs.getString(2));
					decies_case.setDecies_id(rs.getInt(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return decies_case;
	}

	@Override
	public List<Decies_case> findByDecies_id(int decies_id) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM decies_case WHERE state_id = '"
					+ decies_case + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				decies_case.setId(rs.getInt(1));
				decies_case.setName(rs.getString(2));
				decies_case.setDecies_id(rs.getInt(3));
				decies_casesList.add(decies_case);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return decies_casesList;
	}

	@Override
	public void insert(Decies_case o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO decies_case (name, decies_id) VALUE (?, ? )";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());
			ps.setInt(2, o.getDecies_id());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Decies_case", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Decies_case findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM deies WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					decies_case.setId(rs.getInt(1));
					decies_case.setName(rs.getString(2));
					decies_case.setDecies_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return decies_case;
	}

	@Override
	public List<Decies_case> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM decies_case ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				decies_case.setId(rs.getInt(1));
				decies_case.setName(rs.getString(2));
				decies_case.setDecies_id(rs.getInt(3));

				decies_casesList.add(decies_case);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return decies_casesList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM decies_case WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, decies_case.getName());
				ps.setInt(2, decies_case.getDecies_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Decies_case o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE decies_case SET name = '" + o.getName()
					+ " ', decies_id = '" + o.getDecies_id() + "' WHERE id = "
					+ o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
				ps.setInt(2, o.getDecies_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
